package org.colcon.web.model;

import java.util.HashMap;
import java.util.Map;

import org.colcon.web.model.framework.IParam;
import org.colcon.web.model.framework.IParamItem;

public abstract class ParamItem extends Container implements IParamItem
{
	private static final long serialVersionUID = 3841296336014294568L;
	
	private static final String
		PARAMS = "params";
	
	public ParamItem()
	{
		this(new HashMap<String, String>());
	}
	
	public ParamItem(Map<String, String> args)
	{
		super(args);
	}
	
	public ParamItem(String parent, String id) 
	{
		super(parent, id);
		
		addContainer(PARAMS);
	}
	
	public IParam[] getParams()
	{
		return getGrandChildren(PARAMS).toArray(new IParam[0]);
	}

	public boolean hasParam(IParam param)
	{
		return hasParam(param.getId());
	}
	
	public boolean hasParam(String id)
	{
		return has(PARAMS + '/' + id);
	}
	
	public IParam getParam(String id)
	{
		return (IParam) get(PARAMS + '/' + id);
	}
	
	public boolean addParam(String id, Object value)
	{
		return addParam(id, value.toString());
	}
	
	public boolean addParam(String id, String value)
	{
		IParam param = new Param(getPath(), id, value);
		param.setParentPath(getPath());
		return addParam(param);
	}
	
	public boolean addParam(IParam param)
	{
		return add(PARAMS, param);
	}
	
	public String getParamValue(String id)
	{
		return getParam(id).getValue();
	}
}
